{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "875b8c8c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----!"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'sequence': 'the answer to the universe is no.',\n",
       "  'score': 0.16963955760002136,\n",
       "  'token': 2053,\n",
       "  'token_str': 'no'},\n",
       " {'sequence': 'the answer to the universe is nothing.',\n",
       "  'score': 0.07344776391983032,\n",
       "  'token': 2498,\n",
       "  'token_str': 'nothing'},\n",
       " {'sequence': 'the answer to the universe is yes.',\n",
       "  'score': 0.05803241208195686,\n",
       "  'token': 2748,\n",
       "  'token_str': 'yes'},\n",
       " {'sequence': 'the answer to the universe is unknown.',\n",
       "  'score': 0.043957844376564026,\n",
       "  'token': 4242,\n",
       "  'token_str': 'unknown'},\n",
       " {'sequence': 'the answer to the universe is simple.',\n",
       "  'score': 0.04015745222568512,\n",
       "  'token': 3722,\n",
       "  'token_str': 'simple'}]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sagemaker.huggingface import HuggingFaceModel\n",
    "import sagemaker\n",
    "\n",
    "role = sagemaker.get_execution_role()\n",
    "# Hub Model configuration. https://huggingface.co/models\n",
    "hub = {\n",
    "\t'HF_MODEL_ID':'bert-base-uncased',\n",
    "\t'HF_TASK':'fill-mask'\n",
    "}\n",
    "\n",
    "# create Hugging Face Model Class\n",
    "huggingface_model = HuggingFaceModel(\n",
    "\ttransformers_version='4.6.1',\n",
    "\tpytorch_version='1.7.1',\n",
    "\tpy_version='py36',\n",
    "\tenv=hub,\n",
    "\trole=role, \n",
    ")\n",
    "\n",
    "# deploy model to SageMaker Inference\n",
    "predictor = huggingface_model.deploy(\n",
    "\tinitial_instance_count=1, # number of instances\n",
    "\tinstance_type='ml.m5.xlarge' # ec2 instance type\n",
    ")\n",
    "\n",
    "predictor.predict({\n",
    "\t'inputs': \"The answer to the universe is [MASK].\"\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3c9eb30",
   "metadata": {},
   "outputs": [],
   "source": [
    "predictor.delete_endpoint()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "515b9b52",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda_python3",
   "language": "python",
   "name": "conda_python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
